package testGroovy;

import groovy.json.JsonSlurper
import groovy.json.JsonParserType
import testGroovy.model.QuoteFieldStatus

def _MAPPING_DOUBLE_AMOUNT_TYPE = [
	thirdPartyAmount: 'amount02',                                       // 三者险
	driverAmount    : 'amount04',                                       // 车上人员责任险-司机
	passengerAmount : 'amount05',                                       // 车上人员责任险-乘客
	scratchAmount   : 'amount17'                                        // 划痕险
]

def _MAPPING_BOOLEAN_TO_DOUBLE_AMOUNT_TYPE = [
	damage          : 'amount01',                                       // 机动车损失
	theft           : 'amount03',                                       // 盗抢
	spontaneousLoss : 'amount18',                                       // 自燃
	engine          : 'amount41',                                       // 发动机特别损失险
]

def _MAPPING_BOOLEAN_AMOUNT_TYPE = [
	damageIop      : 'amount27',                                        // 机动车损失保险IOP
	thirdPartyIop  : 'amount28',                                        // 第三者责任保险IOP
	theftIop       : 'amount48',                                        // 盗抢险IOP
	driverIop      : 'amount49',                                        // 车上人员责任险-司机IOP
	passengerIop   : 'amount49',                                        // 车上人员责任险-乘客IOP
	scratchIop     : 'amount50',                                        // 车身划痕损失险IOP
	engineIop      : 'amount50'                                         // 发动机特别损失险IOP
]
def _MAPPING_ITEMANDIOP_TYPE = [
	['damage', 'amount01', 'damageIop', 'amount27'],
	['thirdPartyAmount', 'amount02', 'thirdPartyIop', 'amount28'],
	['theft', 'amount03', 'theftIop', 'amount48'],
	['driverAmount', 'amount04', 'driverIop', 'amount49'],
	['passengerAmount', 'amount05', 'passengerIop', 'amount49'],
	['scratchAmount', 'amount17', 'scratchIop', 'amount50',],
	['engine', 'amount41', 'engineIop', 'amount50'],
	['spontaneousLoss', 'amount18','spontaneousLossIop','amount50'] //没有自燃不计免赔,此处为了程序能正常运行
]

def quoteFieldStatus =
[
	createQuoteFieldStatus('scratchAmount', true, 2000, 0, '修改险种对应不计免赔属性'),
	createQuoteFieldStatus('scratchIop', true, 1, 0, '修改险种对应不计免赔属性'),
]

def JSON = '{"pkgName":"optional","errorMsg":"您的车辆暂不能在网上投保!","resultCode":1035,"sameAsLastYear":"0",failRules:{all:{"1":{"autoAdjust":{"other":{},"normal":{"amount02":"200000","amount17":"0"}},"ruleList":[{"context":"取消车身划痕损失险。","direction":"+","flowNo":"1","flowType":"1","groupNo":"1","insuranceCode":"17","limitItems":[],"limitMax":-1,"limitMin":-1,"msg":"取消车身划痕损失险。","operationTips":"amount17","operationType":"D","ruleCode":"AE105","type":"A"},{"context":"增加三者险，多数车主选20万","direction":"+","flowNo":"1","flowType":"1","groupNo":"1","insuranceCode":"","limitItems":[],"limitMax":-1,"limitMin":-1,"msg":"增加商业第三者责任险。","operationTips":"amount02:*","operationType":"A","ruleCode":"AC103","type":"B"},{"context":"增加三者险，多数车主选20万。或者您也可以自己添加盗抢险或司机乘客责任险。","direction":"+","flowNo":"1","flowType":"1","groupNo":"1","insuranceCode":"","limitItems":[],"limitMax":-1,"limitMin":-1,"msg":"增加其他险别，如商业第三者责任险、全车盗抢险等。","operationTips":"amount02:*","operationType":"A","ruleCode":"AO106","type":"B"}]}}}}' as char[]
def jsonParse = new JsonSlurper().with { it ->
	it.type = JsonParserType.LAX
	it.parse(JSON)
}
def failRules = jsonParse.failRules

def allRules = failRules.all.collect { entry ->
	entry.value
}
def autoAdjustMap = allRules.autoAdjust.normal.inject { prev,curr ->
	prev + curr
}
println autoAdjustMap
def ruleList = allRules.ruleList.flatten()
println ruleList

def operationList = ruleList.collect { item ->
	[operationTips: item.operationTips, operationType: item.operationType]
}
println operationList

def SETQFS = { entry, qfs, enabledValue, propName, jsonAmountName ->
	qfs.collect { qfsItem ->
		//map中取出 属性名
		if(entry.key == jsonAmountName) {
			if(qfsItem.filedName == propName) {
				qfsItem.enabled = enabledValue
				def iop = qfs.find { 
					it.filedName == propName.replace('Amount','Iop')
					}
				iop.enabled = enabledValue
			} 
		}
	}
} 
println quoteFieldStatus

autoAdjustMap.each { entry ->
	operationList.collect { item ->
		if(item.operationTips.contains(entry.key)) {
			if(item.operationType == 'D') {
				_MAPPING_DOUBLE_AMOUNT_TYPE.collect(SETQFS.curry(entry, quoteFieldStatus, false))
				_MAPPING_BOOLEAN_TO_DOUBLE_AMOUNT_TYPE.collect(SETQFS.curry(entry, quoteFieldStatus, false))
				_MAPPING_BOOLEAN_AMOUNT_TYPE.collect(SETQFS.curry(entry, quoteFieldStatus, false))
			} else if (item.operationType == 'A') {
				_MAPPING_DOUBLE_AMOUNT_TYPE.collect(SETQFS.curry(entry, quoteFieldStatus, true))
				_MAPPING_BOOLEAN_TO_DOUBLE_AMOUNT_TYPE.collect(SETQFS.curry(entry, quoteFieldStatus, true))
				_MAPPING_BOOLEAN_AMOUNT_TYPE.collect(SETQFS.curry(entry, quoteFieldStatus, true))
			}
		}
	}
}
quoteFieldStatus.each { item ->
	println item.filedName + ':' + item.enabled
}


def static createQuoteFieldStatus(fieldNameValue, enabledValue, originalAmountValue, currentAmountValue, descriptionValue) {
	new QuoteFieldStatus(
		filedName: fieldNameValue,
		enabled: enabledValue,
		originalAmount: originalAmountValue,
		currentAmount: currentAmountValue,
		description: descriptionValue)
}
